package com.hgm.study.ui.components


import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.hgm.study.compositionLocal.LocalUserViewModel
import com.hgm.study.ui.navigation.Destinations
import com.hgm.study.ui.screens.ArticleDetailScreen
import com.hgm.study.ui.screens.LoginScreen
import com.hgm.study.ui.screens.MainFrame
import com.hgm.study.viewmodel.UserViewModel


/**
 * 导航控制器
 */
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun NavHostApp() {

      val navController = rememberAnimatedNavController()

      ProvideWindowInsets {
            CompositionLocalProvider(LocalUserViewModel provides UserViewModel(LocalContext.current)) {

                  //获取viewmodel
                  val userViewModel = LocalUserViewModel.current

                  AnimatedNavHost(
                        navController = navController,
                        startDestination = Destinations.HomeFrame.route
                  ) {
                        //首页大框架
                        composable(
                              route = Destinations.HomeFrame.route,
                              enterTransition = {
                                    slideIntoContainer(AnimatedContentScope.SlideDirection.Right)
                              },
                              exitTransition = {
                                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Left)
                              }
                        ) {
                              MainFrame(
                                    onNavigateToArticle = {
                                          navController.navigate(Destinations.ArticleDetail.route)
                                    },
                                    onNavigateToStudyHistory = {
                                          if (!userViewModel.logged){
                                                //已登录
                                          }else{
                                                //未登录
                                                navController.navigate(Destinations.Login.route)
                                          }
                                    }
                              )
                        }


                        //文章详情页
                        composable(
                              route = Destinations.ArticleDetail.route,
                              enterTransition = {
                                    slideIntoContainer(AnimatedContentScope.SlideDirection.Left)
                              },
                              exitTransition = {
                                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
                              }
                        ) {
                              ArticleDetailScreen(onBack = {
                                    navController.popBackStack()
                              })
                        }


                        //登录
                        composable(
                              route = Destinations.Login.route,
                              enterTransition = {
                                    slideIntoContainer(AnimatedContentScope.SlideDirection.Left)
                              },
                              exitTransition = {
                                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
                              }
                        ) {
                              LoginScreen{
                                    navController.popBackStack()
                              }
                        }
                  }

            }
      }
}

